package cn.mybatis.seeumt.mapper;

import cn.mybatis.seeumt.entity.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserInfoMapperTestCourse {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Test
    public void allEq() {
        Map<String,Object> map = new HashMap<>();
        map.put("gender", 2);
        map.put("age", 16);
//        map.put("third_party_id", null);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq((k,v)->v.toString().equals("2"),map,false);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());


    }

    @Test
    public void eq() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telphone", 663);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
    }

    @Test
    public void ne() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("gender", 2);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
    }

    @Test
    public void gt() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 3);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void ge() { //大于等于
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id", 3);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void lt() { //小于
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("id", 3);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }


    @Test
    public void le() { //小于等于
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("id", 7);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void between() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("age", 16,20);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void notBetween() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.notBetween("age", 16,20);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }


    @Test
    public void like() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name","天");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void notLike() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.notLike("name","天");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void likeLeft() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeLeft("name","哈%");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void likeRight() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name","%天");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void isNull() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("third_party_id");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void isNotNull() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("third_party_id");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void in() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",1,2,3,7,8);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }


    @Test
    public void notIn() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.notIn("id",1,2,3,7,8);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void inSql() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id","select id from user_info where id < 5");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void notInSql() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.notInSql("id","select id from user_info where id < 5");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void groupBy() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("id","name");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void orderByAsc() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void orderByDesc() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void orderBy() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true,true,"gender");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    // TODO: 2019/12/17
    @Test
    public void having() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.having("sum(age) > {0}", 10);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void or() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("age",16).or().eq("gender",1);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void and() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(i -> i.eq("age", "16").ne("gender", "1"));
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void nested() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(i -> i.eq("age", "16").ne("gender", "1"));
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    // TODO: 2019/12/17
    @Test
    public void apply() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(i -> i.eq("age", "16").ne("gender", "1"));
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void last() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("age",16).or().eq("gender",1).last("limit 2");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    // TODO: 2019/12/17
    @Test
    public void exist() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.exists("select id from user_info where gender = 1");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    // TODO: 2019/12/17
    @Test
    public void notExist() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.notExists("select id from user_info where gender = 1");
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }

    @Test
    public void select() {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

        Map<String,Object> map = new HashMap<>();
        map.put("gender", 2);
        map.put("age", 16);
        queryWrapper.select("id", "name");
        queryWrapper.allEq(map);
        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
        System.out.println(maps);
        System.out.println(maps.size());
    }




}
